package com.tech.service.storage.impl;

import com.tech.service.storage.StorageService;
import io.seata.core.context.RootContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Please add the follow VM arguments:
 * <pre>
 *     -Djava.net.preferIPv4Stack=true
 * </pre>
 */
@Slf4j
@DubboService
public class StorageServiceImpl implements StorageService {

    @Resource
    private JdbcTemplate jdbcTemplate;

    /**
     * RootContext.getXID，判定是否在全局事务中
     */
    @Override
    public void deduct(String commodityCode, int count) {
        log.info("Storage Service Begin ... xid: " + RootContext.getXID());
        log.info("Deducting inventory SQL: update storage_tbl set count = count - {} where commodity_code = {}",
            count, commodityCode);

        jdbcTemplate.update("update storage_tbl set count = count - ?, update_time=? where commodity_code = ?",
            new Object[] {count, new Date(), commodityCode});
        log.info("Storage Service End ... ");

    }

}
